﻿using System.Collections.Generic;
using Microsoft.Xna.Framework;

namespace ADiKt {
    class SetupScreen : MenuScreen {
        SpinEntry numberWormsMenuEntry;
        MenuEntry worm;
        MenuEntry back;
        static int numberWorms = Globals.worms.Count;

        public SetupScreen() : base("Setup Details") {
            numberWormsMenuEntry = new SpinEntry("Number of Lines: " + numberWorms);
            numberWormsMenuEntry.Next += numberWormsMenuEntryNext;
            numberWormsMenuEntry.Last += numberWormsMenuEntryLast;
            MenuEntries.Add(numberWormsMenuEntry);

            foreach (Worm w in Globals.worms) {
                string type;
                string col;
                type = (w.GetType() == typeof(Human)) ? "Human" : "AI";
                col = w.colourName;
                worm = new MenuEntry(type + " - " + col);
                worm.Selected += wormSelected;
                MenuEntries.Add(worm);
            }

            back = new MenuEntry("Back");
            back.Selected += OnCancel;
            MenuEntries.Add(back);
        }

        void wormSelected(object sender, PlayerIndexEventArgs e) {
            this.ExitScreen();
            ScreenManager.AddScreen(new WormMenuScreen(Globals.worms[this.selectedEntry-1]), e.PlayerIndex);
        }

        void addWormEntry(Worm w) {
            string type;
            string col;
            type = (w.GetType() == typeof(Human)) ? "Human" : "AI";
            col = w.colourName;
            worm = new MenuEntry(type + " - " + col);
            worm.Selected += wormSelected;
            MenuEntries.Insert(MenuEntries.Count - 1, worm);
        }

        void numberWormsMenuEntryNext(object sender) {
            if (numberWorms < 8) {
                Worm newWorm;
                foreach (KeyValuePair<Color, string> pair in Globals.colListFree) {
                    newWorm = new AI(pair.Key);
                    Globals.worms.Add(newWorm);
                    addWormEntry(newWorm);
                    break;
                }
                numberWorms = Globals.worms.Count;
                numberWormsMenuEntry.Text = "Number of Lines: " + numberWorms;
            }
        }

        void numberWormsMenuEntryLast(object sender) {
            if (numberWorms > 1) {
                Globals.worms[Globals.worms.Count - 1].delete();
                MenuEntries.RemoveAt(MenuEntries.Count - 2);
                numberWorms = Globals.worms.Count;
                numberWormsMenuEntry.Text = "Number of Lines: " + numberWorms;
            }
        }


    }
}
